<?php
class BlogController extends Controller{
	private $db;
	public function __init(){
		$this->db=new BlogModel;
	}
	public function index(){
		$data=$this->db->getAll();
		$this->assign('page',$data['page']);
		$this->assign('data',$data['data']);
		// p($data['data']);exit;
		$this->display('index.html');
	}

	//编辑博客
	public function editBlog(){
		if(IS_POST){
			if($this->db->edit($_POST)){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}else{
			$bid=$_GET['bid'];
			$blog=$this->db->where("bid={$_GET['bid']}")->find();
			$blog['bid']=$bid;
			$category=new CategoryModel;
			$cate=$category->getAll();
			foreach($cate as $k=>$v){
				$cate[$k]['s']=$cate[$k]['cid']==$blog['cid']?" selected='' ":'';
			}
			$this->assign('blog',$blog);
			$this->assign('cate',$cate);
			$this->display('edit.html');
		}
	}

	//添加博客
	public function addBlog(){
		if(IS_POST){
			if($this->db->add($_POST)){
				$this->success('添加博客成功',__CONTROLLER__.'&a=index');
			}else{
				$this->error('添加博客失败',__CONTROLLER__.'&a=index');
			}
		}else{
			$category=new CategoryModel;
			$cate=$category->getAll();
			$this->assign('cate',$cate);
			$this->display('add.html');
		}
	}

	//删除博客
	public function delBlog(){
		if($this->db->del()){
			$this->success('删除博客成功',__MODULE__.'&c=Blog');
		}else{
			$this->error('删除博客失败',__MODULE__.'&c=Blog');
		}
	}
}
?>